import open3d as o3d
import numpy as np

pcd = o3d.io.read_point_cloud("../datasets/standford_cloud_data/China dragon.pcd")
# 点云归一化到单位立方体中
pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
          center=pcd.get_center())

o3d.visualization.draw_geometries([pcd])
# 从点云中创建体素网格
print('voxelization')
voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(pcd,
                                                            voxel_size=0.001)
# 可视化体素网格
o3d.visualization.draw_geometries([voxel_grid])

# 初始化八叉树，最大树深设置为8
octree = o3d.geometry.Octree(max_depth=8)
# 从体素网格中构建八叉树
octree.create_from_voxel_grid(voxel_grid)
# 可视化八叉树
o3d.visualization.draw_geometries([octree])
# 从八叉树转换成网格
voxel_g = octree.to_voxel_grid().create_from_point_cloud(pcd,
                                                         voxel_size=0.01)
# 可视化体素网格
o3d.visualization.draw_geometries([voxel_g])

